package cn.brilliantfuture.commentry.temp.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.log4j.Logger;
import org.jboss.wsf.spi.annotation.WebContext;

import cn.brilliantfuture.commentry.temp.RangeTempMessageRemote;
import cn.brilliantfuture.commentry.temp.RangeTempMessageWebService;

/**
 * 
 * Title: Communication Entry
 * 
 * Author:  LiTian  
 *
 * Date: 2011-11-2
 * 
 * Description: 申请恒温范围类
 * 
 */
@Stateless
@WebService(endpointInterface = "cn.brilliantfuture.commentry.temp.RangeTempMessageWebService")
@WebContext(contextRoot = "/commentry", urlPattern = "/RangeTempMessageWebService")
public class RangeTempMessageImpl implements RangeTempMessageRemote,
		RangeTempMessageWebService {
	
	protected static final Logger logger = Logger.getLogger(RangeTempMessageImpl.class); 
	
	@PersistenceContext(unitName = "commentry-jpa")
	private EntityManager entityManager;

	@SuppressWarnings("unchecked")
	public short[] getRangeTemp(String deviceSn, int gridNo, String rfidSn) {
		
		logger.info("RangeTempMessageImpl.getRangeTemp(String deviceSn, int gridNo, String rfidSn)");
		logger.info("deviceSn -> " + deviceSn + " gridNo -> " + gridNo + " rfidSn -> " + rfidSn);
		
		short[] result = null;
		
		StringBuffer sql = new StringBuffer();
		sql.append("select oi.data_model_value from storage_grid_purchaser_news sgpn inner join order_information oi on sgpn.order_information_number = oi.number");
		sql.append(" where sgpn.device_sn = '");
		sql.append(deviceSn);
		sql.append("' and sgpn.rfid_sn = '");
		sql.append(rfidSn);
		sql.append("'");
		
		List<Object> objs = entityManager.createNativeQuery(sql.toString()).getResultList();
		
		if (null != objs)
			if (objs.size() > 0){		
				String[] rangeValues = objs.get(0).toString().split(",");
				
				if (rangeValues.length == 4){
					result = new short[rangeValues.length];
					for (int i = 0; i < result.length; i++)
						result[i] = Short.parseShort(String.valueOf(Float.parseFloat(rangeValues[i]) * 100).split("\\.")[0]);
				}
			}
		
		return result;
	}

}
